﻿#include <QFile>
#include "CSensorSeriesHistory.h"
#include "CGlobal.h"
#include "CSqlOper.h"
#include "Public.h"
#include "CVideoResultQuery.h"

CSensorSeriesHistory::CSensorSeriesHistory(QString sDevGuid, QWidget *parent)
	: m_sDevGuid(sDevGuid),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("历史记录查询"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	//setStyleSheetFile();
	initConnect();
	initUi();
}

CSensorSeriesHistory::~CSensorSeriesHistory()
{}

void CSensorSeriesHistory::setStyleSheetFile()
{
	this->setWindowTitle(A2T("历史记录查询"));
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CSensorSeriesHistory::initConnect()
{
	connect(ui.widget, &QMyTableView::sigclose, this, &CSensorSeriesHistory::slotClose);
	connect(ui.pushButtonQuery, &QPushButton::clicked, this, &CSensorSeriesHistory::slotQuery);
	connect(ui.pushButtonResult, &QPushButton::clicked, this, &CSensorSeriesHistory::slotResult);
}

void CSensorSeriesHistory::initUi()
{
	QStringList headerListAction;		//水平表头
	headerListAction << A2T("序号") << A2T("通道") << A2T("采集时间") << A2T("维度") <<A2T("诊断结果") << A2T("匹配度") << A2T("温度诊断") <<
		A2T("振动诊断") << A2T("声音诊断") << A2T("加速度");

	QList<quint16> listColumnWidth;		//列宽
	listColumnWidth << 50 << 100 << 200 << 50 << 100 << 100 << 100 << 100 << 100 << 100;
	//设置列数
	ui.widget->m_pModel->setColumnCount(headerListAction.count());
	//设置表头
	ui.widget->m_pModel->setHorizontalHeaderLabels(headerListAction);
	ui.widget->ui.tableView->setModel(ui.widget->m_pModel);

	//设置列宽
	for (int i = 0; i < listColumnWidth.count(); i++)
	{
		ui.widget->ui.tableView->setColumnWidth(i, listColumnWidth.at(i));
	}

	QDateTime timeTo = QDateTime::currentDateTime();
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = QDateTime::currentDateTime();
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	ui.dateTimeEditBegin->setCalendarPopup(true);
	ui.dateTimeEditEnd->setCalendarPopup(true);
}

void CSensorSeriesHistory::slotClose()
{
	this->close();
}

void CSensorSeriesHistory::slotQuery()
{
	//首先清空列表
	ui.widget->m_pModel->removeRows(0, ui.widget->m_pModel->rowCount());
	QString sStartTime = ui.dateTimeEditBegin->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QString sEndTime = ui.dateTimeEditEnd->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QString sErrMsg = "";
	QVector<QSqlRecord> vecData;
	CSqlOper::instance().selectHisData(m_sDevGuid, sStartTime, sEndTime, vecData, sErrMsg);
	int nNo = 1;
	//刷新状态
	int nCount = vecData.size();
	ui.widget->UpdateStatus(nCount);
	for (int i = 0; i < vecData.size(); i++)
	{
		QList<QStandardItem*> item;
		item.append(new QStandardItem(QString::number(nNo++)));//序号
		item.append(new QStandardItem(vecData[i].value("CHANNEL_NAME").toString()));
		item.append(new QStandardItem(vecData[i].value("TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss")));
		if (vecData[i].value("DATA_DIRECTION").toString() == "1")
		{
			item.append(new QStandardItem(A2T("x轴")));
		}
		else if (vecData[i].value("DATA_DIRECTION").toString() == "2")
		{
			item.append(new QStandardItem(A2T("y轴")));
		}
		else if (vecData[i].value("DATA_DIRECTION").toString() == "3")
		{
			item.append(new QStandardItem(A2T("z轴")));
		}
		else
		{
			item.append(new QStandardItem(A2T("")));
		}

		if (vecData[i].value("RESULT").toString() == "0")
		{
			item.append(new QStandardItem(A2T("正常")));
		}
		else
		{
			item.append(new QStandardItem(A2T("异常")));
		}

		item.append(new QStandardItem(vecData[i].value("MATCH_LEVEL").toString()));

		if (vecData[i].value("TEMP_RESULT").toString() == "1")
		{
			item.append(new QStandardItem(A2T("异常")));
		}
		else
		{
			item.append(new QStandardItem(A2T("正常")));
		}
		if (vecData[i].value("RMS_RESULT").toString() == "1")
		{
			item.append(new QStandardItem(A2T("异常")));
		}
		else
		{
			item.append(new QStandardItem(A2T("正常")));
		}
		if (vecData[i].value("VOICE_RESULT").toString() == "1")
		{
			item.append(new QStandardItem(A2T("异常")));
		}
		else
		{
			item.append(new QStandardItem(A2T("正常")));
		}
		item.append(new QStandardItem(vecData[i].value("RMS").toString()));

		//设置居中显示
		for (int nRow = 0; nRow < item.count(); nRow++)
		{
			item.at(nRow)->setTextAlignment(Qt::AlignCenter);
			//item.at(nRow)->setForeground(QBrush(QColor(255, 255, 255)));
		}
		ui.widget->m_pModel->insertRow(ui.widget->m_pModel->rowCount(), item);
	}
}

void CSensorSeriesHistory::slotResult()
{
	CVideoResultQuery oper;
	oper.exec();
}
